// pages/info/index.js
import {
    form
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        columns: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
        formList: [],
        cascaderValue: '',
        options: [],
        fieldNames: {
            text: 'label',
            value: 'value',
            children: 'children',
        },
        obj: {},
        inputList: [],
        id: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getForm(options.id)
        this.setData({
            id: options.id
        })
    },
    getForm(id) {
        form.detail({
            form_id: id
        }, res => {
            if (res.code == 1) {
                let datas = JSON.parse(res.data.rules)
                datas.forEach(item => {
                    item.value = ''
                })
                console.log(datas)
                let options = []
                if(id==1){
                    if (datas[1].type == "cascader") {
                        options = datas[1].props.options
                    } else {
                        options = datas[1].options
                    }
                }else{
                    if (datas[2].type == "cascader") {
                        options = datas[2].props.options
                    } else {
                        options = datas[2].options
                    }
                }
               
                this.setData({
                    formList: datas,
                    options: options
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    submit(e) {
        let datas = JSON.stringify(e.detail.value)
        this.setData({
            obj: datas
        })

        if (this.data.formList[0].value == '' || this.data.formList[1].value == '') {
            wx.showToast({
                title: '请输入完整信息',
                icon: "error"
            })
        } else {
            if(this.data.id==2){
                if(this.data.formList[2].value == ''){
                    wx.showToast({
                        title: '请输入完整信息',
                        icon: "error"
                    })
                    return false
                }
                let obj1 = {}
                let obj2 = {}
                let obj3 = {}
                let key = this.data.formList[0].field
                let key2 = this.data.formList[1].field
                let key3 = this.data.formList[2].field
                let value = this.data.formList[0].value
                let value2 = this.data.formList[1].value
                let value3 = this.data.formList[2].value
                obj1[key] = value
                obj2[key2] = value2
                obj3[key3] = value3
                let obj = JSON.stringify(Object.assign({}, obj1, obj2, obj3));
                console.log(obj);
                wx.navigateTo({
                    url: '/pages/question/index?obj=' + obj + '&id=' + this.data.id,
                })
            }else{
                let obj1 = {}
                let obj2 = {}
                let key = this.data.formList[0].field
                let key2 = this.data.formList[1].field
                let value = this.data.formList[0].value
                let value2 = this.data.formList[1].value
                obj1[key] = value
                obj2[key2] = value2
                let obj = JSON.stringify(Object.assign({}, obj1, obj2));
                console.log(obj);
                wx.navigateTo({
                    url: '/pages/question/index?obj=' + obj + '&id=' + this.data.id,
                })
            }
          
        }

    },
    onCancel() {
        this.setData({
            show: false
        })
    },
    onConfirm(e) {
        console.log(e)
        const {
            selectedOptions,
            value
        } = e.detail;
        const fieldValue = selectedOptions
            .map((option) => option.label || option.name)
            .join('/');
        let arr = []
        selectedOptions.forEach(item => {
            arr.push(item.value)
        })
        if(this.data.id==1){
            if(this.data.formList[1].type=="select"){
                arr=arr.toString()
            }
            this.setData({
                ['formList[1].value']: arr
            })
        }else{
            if(this.data.formList[2].type=="select"){
                arr=arr.toString()
            }
            this.setData({
                ['formList[2].value']: arr
            })
        }
        console.log(this.data.formList)
       
        this.setData({
            fieldValue,
            cascaderValue: value,
            show: false,
        })
    },
    // 获取文本输入值
    onInputChange(e) {
        const that = this;
        console.log(e)
        let index = e.currentTarget.dataset.index
        let {
            detail
        } = e;
        that.setData({
            ['formList['+index+'].value']: detail.value
        })
    },
    toShow() {
        this.setData({
            show: true
        })
    },
    toUrl() {
        wx.navigateTo({
            url: '/pages/question/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})